import { Component, OnDestroy } from '@angular/core';

import { Util } from '../../../utils/util';

import { WordDetailHistoryService } from './word.history.service';

@Component({
  selector: 'word-history',
  templateUrl: './word.history.html',
  styles: [`
  `],
  providers: [WordDetailHistoryService]
})


export class WordDetailHistoryComponent {

  wordInfo: any;
  constructor(private util: Util, private service: WordDetailHistoryService) {

   
    this.wordInfo = this.util.sessionStore().get('wordDetail');

    console.log(this.wordInfo);

    this.vm.loading = true;

    this.getHttp(this.wordInfo, {});
  }

  vm = {
    "loading": true,
    "fileName":'',
    "data": [],
    "type": "line-word"
  };

  columns = ['时间', '日销售额', '日销售量', '近30天销售额', '近30天销售量', '动销宝贝数','最热指数','最新指数'];


  ngOnDestroy() {
    if (this.offEvent) {
      this.offEvent.unsubscribe();

    }
  }


  export() {
    console.log("export");

    let dim = ['date', 'words', 'categoryName', 'sellMoney', 'sellAmount', 'monthSellMoney', 'monthSellAmount', 'productNum', 'productSellMoneyGrowth', 'productSellMoneyDecrease', 'sellGrowthProductNum', 'sellDecreaseProductNum', 'hotIndexNumber', 'growthIndexNumber'];

    let dimText = ['时间', '词云', '类目', '日销售额', '日销售量', '近30天销售额', '近30天销售量', '动销宝贝数', 'productSellMoneyGrowth', 'productSellMoneyDecrease', 'sellGrowthProductNum', 'sellDecreaseProductNum', 'hotIndexNumber'];

    let data = this.util.jsonToArrry(this.vm.data, dim, dimText);
    let sheetName: string;
    try {
      sheetName = this.httpParam.begin.slice(0, 10) + '~' + this.httpParam.end.slice(0, 10) + this.wordInfo.words;
    } catch (e) {
      sheetName = this.wordInfo.words;
    }

    this.util.export(data, sheetName);
  }

  // 日期的处理
  dateOnChange(data) {

    // console.log(data);

    this.vm.loading = true;

    this.getHttp(this.wordInfo, data);

  }



  httpParam: any;
  offEvent: any;
  getHttp(wordInfo, date) {

    let json = {
      word: wordInfo.words,
      categoryId: wordInfo.categoryId,
      type:wordInfo.type
    };

    Object.assign(json, date);

    this.httpParam = json;

    this.offEvent = this.service.get(json).subscribe((res: any) => {

      // this.vm.fileName = this.httpParam.begin.slice(0, 10) + '~' + this.httpParam.end.slice(0, 10) + this.wordInfo.words;

      this.vm.data = res.data;

      this.vm.loading = false;

    }, () => { });


  }

}
